#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<list>
#include<assert.h>
using namespace std;
namespace mylist
{
	template<class T>
	struct listnode
	{
		listnode* _next;
		listnode* _prev;
		T _val;
		listnode(const T& val = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _val(val)
		{};
	};

	template<class T,class Ref,class Ptr>
	struct list_iterator
	{
		typedef listnode<T> node;
		typedef list_iterator<T, Ref, Ptr> self;
		list_iterator(node* pnode = nullptr)
		{
			_node = pnode;
		}
		list_iterator(const self& li)
		{
			_node = li._node;
		}
		Ref operator*()
		{
			return _node->_val;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(_node);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& li) const
		{
			return _node != li._node;
		}
		bool operator==(const self& li) const
		{
			return _node == li._node;
		}
		node* _node;
	};


	template<class Iterator, class Ref, class Ptr>
	struct reverse_list_iterator
	{
	public:
		Iterator _rnode;
		typedef reverse_list_iterator<Iterator, Ref, Ptr> riterator;
		reverse_list_iterator(Iterator it)
			:_rnode(it)
		{}
		Ref operator*()
		{
			Iterator tmp = _rnode;
			--tmp;
			return *tmp;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		riterator& operator++()
		{
			--_rnode;
			return *this;
		}
		riterator operator++(int)
		{
			riterator tmp(_rnode);
			_rnode--;
			return tmp;
		}
		riterator& operator--()
		{
			++_rnode;
			return *this;
		}
		riterator operator--(int)
		{
			riterator tmp(_rnode);
			_rnode++;
			return tmp;
		}
		bool operator!=(const riterator& rit)
		{
			return _rnode != rit._rnode;
		}
		bool operator==(const riterator& rit)
		{
			return _rnode == rit._rnode;
		}
	};
	/*template<class T>
	struct _const_list_iterator
	{
		typedef listnode<T> node;
		typedef _const_list_iterator<T> const_iterator;
		_const_list_iterator(node* pnode = nullptr)
		{
			_node = pnode;
		}
		_const_list_iterator(const const_iterator& li)
		{
			_node = li._node;
		}
		const T& operator*()
		{
			return _node->_val;
		}
		const T* operator->()
		{
			return &(operator*());
		}
		const_iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		const_iterator operator++(int)
		{
			iterator tmp(_node);
			_node = _node->_next;
			return tmp;
		}
		const_iterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		const_iterator operator--(int)
		{
			iterator tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const const_iterator& li)
		{
			return _node != li._node;
		}
		bool operator==(const const_iterator& li)
		{
			return _node == li._node;
		}
		node* _node;
	};*/

	template<class T>
	class list
	{
		typedef listnode<T> node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		typedef reverse_list_iterator<iterator, T&, T*> reverse_iterator;
		typedef reverse_list_iterator<const_iterator, const T&, const T*> const_reverse_iterator;
		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(end());
		}
		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(begin());
		}
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}

		list<T>& operator=(list<T> x)
		{
			swap(x);
			return *this;
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		size_t size() const
		{
			size_t num = 0;
			iterator cur = _head->_next;
			while (cur != end())
			{
				cur++;
				num++;
			}
			return num;
		}

		bool empty() const
		{
			return _head->_next == _head;
		}

		void clear()
		{
			iterator cur = begin();
			node* tmp = cur._node;
			while (cur != end())
			{
				tmp = cur._node;
				cur++;
				delete tmp;
			}
			_head->_next = _head;
			_head->_prev = _head;
		}

		iterator insert(iterator pos, const T& val)
		{
			node* newnode = new node(val);
			node* cur = pos._node;
			newnode->_next = cur;
			newnode->_prev = cur->_prev;
			cur->_prev->_next = newnode;
			cur->_prev = newnode;
			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			assert(!empty());
			node* cur = pos._node;
			node* next = cur->_next;
			cur->_next->_prev = cur->_prev;
			cur->_prev->_next = cur->_next;
			delete cur;
			return iterator(next);
		}

		void swap(list<T>& l)
		{
			std::swap(_head, l._head);
		}

		list()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		template<class iiterator>
		list(iiterator first, iiterator last)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		list(const list<T>& l)
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head; 
			list<T> tmp(l.begin(), l.end());
			swap(tmp);
		}

		~list()
		{
			clear();
			delete _head;
		}
	private:
		node* _head;
	};
};